package service

import (
	"context"
	"gitee.com/hxchjm/go-admin/internal/dao"
	"gitee.com/hxchjm/go-admin/internal/models"
	"gitee.com/hxchjm/go-admin/pkg/ecode"
	"gitee.com/hxchjm/go-admin/pkg/jwtauth"
	"github.com/jinzhu/copier"
)

type Dept struct {
}

// GetPage
// @Summary 分页部门列表数据
// @Description 分页列表
// @Tags 部门
// @Param deptName query string false "deptName"
// @Param deptId query string false "deptId"
// @Param position query string false "position"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dept [get]
// @Security Bearer
func (e Dept) GetPage(ctx context.Context, req *models.ReqDeptGetPage) (any, error) {
	list, err := dao.SysDept{}.SetDeptPage(ctx)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// Get
// @Summary 获取部门数据
// @Description 获取JSON
// @Tags 部门
// @Param deptId path string false "deptId"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dept/{deptId} [get]
// @Security Bearer
func (e Dept) Get(ctx context.Context, req *models.ReqDeptGet) (any, error) {
	res, err := (dao.SysDept{SysDept: models.SysDept{DeptId: req.Id}}).Get(ctx)
	if err != nil {
		return nil, err
	}
	return res, nil
}

// Insert 添加部门
// @Summary 添加部门
// @Description 获取JSON
// @Tags 部门
// @Accept  application/json
// @Product application/json
// @Param data body dto.SysDeptInsertReq true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dept [post]
// @Security Bearer
func (e Dept) Insert(ctx context.Context, req *models.ReqDeptInsert) (any, error) {
	req.CreateBy = jwtauth.GetClaims(ctx).UID
	var sd = &dao.SysDept{}
	_ = copier.Copy(&sd, req)
	if err := sd.Insert(ctx); err != nil {
		return nil, err
	}
	return &models.RspDeptInsert{Id: sd.DeptId}, nil
}

// Update
// @Summary 修改部门
// @Description 获取JSON
// @Tags 部门
// @Accept  application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.SysDeptUpdateReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dept/{deptId} [put]
// @Security Bearer
func (e Dept) Update(ctx context.Context, req *models.ReqDeptUpdate) (any, error) {
	req.UpdateBy = jwtauth.GetClaims(ctx).UID
	var sd dao.SysDept
	_ = copier.Copy(&sd, req)
	if err := sd.Update(ctx); err != nil {
		return nil, err
	}
	return &models.RspDeptInsert{Id: sd.DeptId}, nil
}

// Delete
// @Summary 删除部门
// @Description 删除数据
// @Tags 部门
// @Param data body dto.SysDeptDeleteReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/v1/dept [delete]
// @Security Bearer
func (e Dept) Delete(ctx context.Context, req *models.ReqDeptDelete) (any, error) {
	if len(req.Ids) <= 0 {
		return nil, ecode.Wrap(ecode.ERRParam, "ids shouldn't be empty")
	}
	if err := (dao.SysDept{SysDept: models.SysDept{DeptId: req.Ids[0]}}).Delete(ctx); err != nil {
		return nil, err
	}
	return nil, nil
}

// Get2Tree 用户管理 左侧部门树
func (e Dept) Get2Tree(ctx context.Context, req *models.ReqDeptGetPage) (any, error) {
	var sd dao.SysDept
	_ = copier.Copy(&sd, req)
	list, err := sd.SetDeptTree(ctx)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// GetDeptTreeRoleSelect TODO: 此接口需要调整不应该将list和选中放在一起
func (e Dept) GetDeptTreeRoleSelect(ctx context.Context, req *models.ReqGetDeptTreeRoleSelect) (any, error) {
	sd := dao.SysDept{}
	result, err := sd.SetDeptLabel()
	if err != nil {
		return nil, err
	}
	menuIds := make([]int, 0)
	if req.Id != 0 {
		menuIds, err = sd.GetWithRoleId(req.Id)
		if err != nil {
			return nil, err
		}
	}
	rsp := &models.RspGetDeptTreeRoleSelect{
		Depts:       result,
		CheckedKeys: menuIds,
	}
	return rsp, nil
}
